package com.xhui.vimarket.zkplus.zul.attr.mod;
import java.util.HashMap;

import org.slf4j.Logger;
import org.zkoss.zk.ui.Component;

import com.xhui.vimarket.zkplus.zul.attr.bean.impl.AttrTmplImpl;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBandComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBatchWarperComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBusProcComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrCheckboxByInstComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrCheckboxComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrComboboxComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrDateComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrDatetimeComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrDiscountComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrDivComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrDivExtComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrDoubleComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrGridComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrGroupboxComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrIntComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrLabelComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrLayoutComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrMenuTextComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrMoneyComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrMultiCheckboxComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrMultiSubComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrNullComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrNullValComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrPanelComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrPanelGridComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrPasswordComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrRadioComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrSelectTreeComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrSelectlistComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrShowSettingComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTabboxComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTabboxContentComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTabboxTbarComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTextComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTextInfoComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTimeComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTreeTabComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrWarperComp;

/**
 * 属性控件工厂.
 * 
 * @版权：福富软件 版权所有 (c) 2011
 * @author wuyx
 * @version Revision 1.0.0
 * @see:
 * @创建日期：2011-1-23
 * @功能说明：
 * 
 */
public final class AttrCompFactory {
    
    /**
     * 日志.
     */
    private static Logger                          logger   = Logger
                                                                .getLogger(AttrCompFactory.class);
    
    /**
     * 控件map.
     */
    private static final HashMap<String, AttrComp> COMP_MAP = new HashMap<String, AttrComp>();
    
    /**
     * 私有构造函数.
     */
    private AttrCompFactory() {
    }
    
    /**
     * 注册控件.
     * 
     * @param key String
     * @param comp AttrComp
     * @author wuyx
     * 将控件加入到控件map中
     */
    public static void registComp(final String key, final AttrComp comp) {
        AttrCompFactory.COMP_MAP.put(key, comp);
    }
    
    /**
     * 获取控件生成器实例.
     * @param displayMode String
     * @return AttrComp
     * @author wuyx
     * 从控件map中获取控件生成器实例
     */
    public static AttrComp getInst(final String displayMode) {
        if (COMP_MAP == null || COMP_MAP.size() < 1) {
            AttrCompFactory.init();
        }
        AttrCompFactory.logger.debug(displayMode);
        if (AttrCompFactory.COMP_MAP.containsKey(displayMode)) {
            return AttrCompFactory.COMP_MAP.get(displayMode);
        } else {
            AttrCompFactory.logger.debug("use null comp");
            return AttrNullComp.instance();
        }
    }
    
    /**
     * 获取控件生成器实例.
     * @param comp Component
     * @return AttrComp
     * @author wuyx
     * 从控件map中获取控件生成器实例
     */
    public static AttrComp getInstByComponent(final Component comp) {
        final Object oMode = comp == null ? null
            : comp.getAttribute(AttrBaseComp.COMP_MODE);
        
        if (oMode == null) {
            AttrCompFactory.logger.debug("use null comp");
            return AttrNullComp.instance();
        } else if (AttrCompFactory.COMP_MAP.containsKey(oMode + "")) {
            return AttrCompFactory.COMP_MAP.get(oMode + "");
        } else {
            AttrCompFactory.logger.debug("use null comp");
            return AttrNullComp.instance();
        }
    }
    
    /**
     * 初始化控件工厂.
     * @author wuyx
     * 将已实现控件实例注册到工厂类
     */
    public static void init() {
        AttrCompFactory.logger.debug("init");
        // null
        AttrCompFactory.registComp(AttrTmplImpl.MODE_NULL, AttrNullComp.instance());
        // null
        AttrCompFactory.registComp(AttrTmplImpl.MODE_NUL, AttrNullComp.instance());
        // NULLVAL
        AttrCompFactory.registComp(AttrTmplImpl.MODE_NULLVAL, AttrNullValComp.instance());
        // warper
        AttrCompFactory.registComp(AttrTmplImpl.MODE_WARPER, AttrWarperComp.instance());
        // warper
        AttrCompFactory.registComp(AttrTmplImpl.MODE_BATCHWARPER, AttrBatchWarperComp.instance());
        // layout
        AttrCompFactory.registComp(AttrTmplImpl.MODE_LAYOUT, AttrLayoutComp.instance());
        // multi
        AttrCompFactory.registComp(AttrTmplImpl.MODE_MULTISUB, AttrMultiSubComp.instance());
        // grid
        AttrCompFactory.registComp(AttrTmplImpl.MODE_GRID, AttrGridComp.instance());
        // panel
        AttrCompFactory.registComp(AttrTmplImpl.MODE_PANEL, AttrPanelComp.instance());
        // div
        AttrCompFactory.registComp(AttrTmplImpl.MODE_DIV, AttrDivComp.instance());
        // panelgrid
        AttrCompFactory.registComp(AttrTmplImpl.MODE_PANELGRID, AttrPanelGridComp.instance());
        // groupbox
        AttrCompFactory.registComp(AttrTmplImpl.MODE_GROUPBOX, AttrGroupboxComp.instance());
        // treetab
        AttrCompFactory.registComp(AttrTmplImpl.MODE_TREETAB, AttrTreeTabComp.instance());
        // tabbox
        AttrCompFactory.registComp(AttrTmplImpl.MODE_TABBOX, AttrTabboxComp.instance());
        // tabboxContent
        AttrCompFactory.registComp(AttrTmplImpl.MODE_TABCONTENT, AttrTabboxContentComp.instance());
        // tabbocTbar
        AttrCompFactory.registComp(AttrTmplImpl.MODE_TABTBAR, AttrTabboxTbarComp.instance());
        // label
        AttrCompFactory.registComp(AttrTmplImpl.MODE_LABEL, AttrLabelComp.instance());
        // bandbox
        AttrCompFactory.registComp(AttrTmplImpl.MODE_BANDBOX, AttrBandComp.instance());
        // checkbox
        AttrCompFactory.registComp(AttrTmplImpl.MODE_CHECKBOX, AttrCheckboxComp.instance());
        // combobox
        AttrCompFactory.registComp(AttrTmplImpl.MODE_COMBOBOX, AttrComboboxComp.instance());
        // date
        AttrCompFactory.registComp(AttrTmplImpl.MODE_DATE, AttrDateComp.instance());
        // datetime
        AttrCompFactory.registComp(AttrTmplImpl.MODE_DATETIME, AttrDatetimeComp.instance());
        // time
        AttrCompFactory.registComp(AttrTmplImpl.MODE_TIME, AttrTimeComp.instance());
        // int
        AttrCompFactory.registComp(AttrTmplImpl.MODE_INT, AttrIntComp.instance());
        // selectlist
        AttrCompFactory.registComp(AttrTmplImpl.MODE_SELECTLIST, AttrSelectlistComp.instance());
        // selectlist
        AttrCompFactory.registComp(AttrTmplImpl.MODE_CHECKBOX_BYINST, AttrCheckboxByInstComp
            .instance());
        // multi checkbox
        AttrCompFactory.registComp(AttrTmplImpl.MODE_MULTI_CHECKBOX, AttrMultiCheckboxComp
            .instance());
        // radio
        AttrCompFactory.registComp(AttrTmplImpl.MODE_RADIO, AttrRadioComp.instance());
        // text
        AttrCompFactory.registComp(AttrTmplImpl.MODE_TEXT, AttrTextComp.instance());
        // text_info
        AttrCompFactory.registComp(AttrTmplImpl.MODE_TEXT_INFO, AttrTextInfoComp.instance());
        // buttontext
        AttrCompFactory.registComp(AttrTmplImpl.MODE_BUTTONTEXT, AttrButtonTextComp.instance());
        // menutext
        AttrCompFactory.registComp(AttrTmplImpl.MODE_MENUTEXT, AttrMenuTextComp.instance());
        // password
        AttrCompFactory.registComp(AttrTmplImpl.MODE_PASSWORD, AttrPasswordComp.instance());
        // password two input
        AttrCompFactory.registComp(AttrTmplImpl.MODE_PASSWORDTWO, AttrPasswordTwoComp.instance());
        // showsetting
        AttrCompFactory.registComp(AttrTmplImpl.MODE_SHOWSETTING, AttrShowSettingComp.instance());
        // divext
        AttrCompFactory.registComp(AttrTmplImpl.MODE_DIVEXT, AttrDivExtComp.instance());
        // divext
        AttrCompFactory.registComp(AttrTmplImpl.MODE_BUS_PROC, AttrBusProcComp.instance());
        
        // liuzhuangfei 2011-10-11 add for select tree compente
        AttrCompFactory.registComp(AttrTmplImpl.MODE_SELECTTREE, AttrSelectTreeComp.instance());
        //g.luxb 2011-11-17 add for double
        AttrCompFactory.registComp(AttrTmplImpl.MODE_DOUBLE, AttrDoubleComp.instance());
        // money
        AttrCompFactory.registComp(AttrTmplImpl.MODE_MONEY, AttrMoneyComp.instance());
        // Discount
        AttrCompFactory.registComp(AttrTmplImpl.MODE_DISCOUNT, AttrDiscountComp.instance());
    }
}
